﻿using System;
using System.Configuration;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using CloudShot.Utils;

namespace CloudShot
{
	public sealed class AppSettings : ApplicationSettingsBase
	{
		#region Public Properties

		//[UserScopedSettingAttribute]
		//public Type ActiveStorage
		//{
		//  get { return (Type)this["ActiveStorage"]; }
		//  set { this["ActiveStorage"] = value; }
		//}

		[UserScopedSettingAttribute]
		public string PathToSave
		{
			get
			{
				string pathToSave = (string)this["PathToSave"];
				return string.IsNullOrEmpty(pathToSave) ? GetDefaultSaveDirectory() : pathToSave;
			}
			set { this["PathToSave"] = value; }
		}

		[UserScopedSettingAttribute]
		[DefaultSettingValueAttribute("True")]
		public bool StartWithComputer
		{
			get { return ConvertToBool(this["StartWithComputer"]); }
			set { this["StartWithComputer"] = value; }
		}

		[UserScopedSettingAttribute]
		[DefaultSettingValueAttribute("True")]
		public bool OpenInBrowser
		{
			get { return ConvertToBool(this["OpenInBrowser"]); }
			set { this["OpenInBrowser"] = value; }
		}

		[UserScopedSettingAttribute]
		[DefaultSettingValueAttribute("True")]
		public bool CopyUrl
		{
			get { return ConvertToBool(this["CopyUrl"]); }
			set { this["CopyUrl"] = value; }
		}

		[UserScopedSettingAttribute]
		[DefaultSettingValueAttribute("True")]
		public bool CopyImage
		{
			get { return ConvertToBool(this["CopyImage"]); }
			set { this["CopyImage"] = value; }
		}

		[UserScopedSettingAttribute]
		[DefaultSettingValueAttribute("PrintScreen")]
		public string ShotHotkey
		{
			get { return ((string)(this["ShotHotkey"])); }
			set { this["ShotHotkey"] = value; }
		}

		[UserScopedSettingAttribute]
		[DefaultSettingValueAttribute("Png")]
		public ImageFormat ImageFormat
		{
			get
			{
				ImageFormat result = ((ImageFormat)(this["ImageFormat"]));
				return result ?? ImageFormat.Png;
			}
			set { this["ImageFormat"] = value; }
		}

		[UserScopedSettingAttribute]
		[SettingsSerializeAs(SettingsSerializeAs.Binary)]
		public ShotInfo[] RecentShotsList
		{
			get
			{
				ShotInfo[] result = (ShotInfo[])this["RecentShotsList"];

				if (result == null)
					result = new ShotInfo[0];

				return result;
			}
			set { this["RecentShotsList"] = value; }
		}

		#endregion

		#region Overidings

		protected override void OnSettingsLoaded(object sender, SettingsLoadedEventArgs e)
		{
			base.OnSettingsLoaded(sender, e);
			UpdateStartupShortcut();
		}

		protected override void OnSettingsSaving(object sender, System.ComponentModel.CancelEventArgs e)
		{
			base.OnSettingsSaving(sender, e);
			UpdateStartupShortcut();
		}

		#endregion

		#region Helper Methods

		private void UpdateStartupShortcut()
		{
			RegistryUtils.SetStartup(Application.ProductName, StartWithComputer);
		}

		private static string GetDefaultSaveDirectory()
		{
			string folder = DropboxUtils.DropboxPublicFolder;

			if (!Directory.Exists(folder))
				folder = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

			string pathToSave = Path.Combine(folder, Application.ProductName);
			Directory.CreateDirectory(pathToSave);
			return pathToSave;
		}

		private bool ConvertToBool(object value)
		{
			if (value is bool)
				return (bool)value;

			return false;
		}

		#endregion
	}
}